<?php
/**
 * Created by IntelliJ IDEA.
 * User: Jason
 * Date: 2018-12-03
 * Time: 9:22
 */

namespace app\member\controller;
use cmf\controller\AdminBaseController;
use think\Db;

/**
 * 会员控制器
 * Class AdminMemberController
 * @package app\member
 */
class AdminMemberController extends AdminBaseController
{

    /**
     * 会员列表
     */
    public function index()
    {
        $member_name = request()->param('member_name');
        $taobao_id = request()->param('taobao_id');
        $where =['status'=>1000,'mark'=>2000];

        if($member_name){
            $this->assign('member_name',$member_name);
            $where['member_name']=['like','%'.$member_name.'%'];
        }
        if($taobao_id){
            $this->assign('taobao_id',$taobao_id);
            $where['taobao_id']=['like','%'.$taobao_id.'%'];
        }

        $member = Db::name('member_member')->where($where)->paginate(20);
        $page= $member->render();

        $this->assign('member',$member);
        $this->assign('page',$page);
        return $this->fetch();
    }


    /**
     * 添加视图
     */
    public function add()
    {
        return $this->fetch();
    }
    /**
     * 添加会员
     * member_name
     * taobao_id
     * phone
     */
    public function addPost()
    {
        $now = time();
        $params = $this->request->param();
        $params['mark'] = 2000;
        $params['status'] = 1000;
        $params['add_time'] = $now;
        $params['update_time'] = $now;
        $res = Db::name('member_member')->insert($params);
        if($res)
            return $this->success("添加成功！",url('AdminMember/index'));
        else
            return $this->error('添加失败');
    }

    /**
     * 编辑视图
     */
    public function edit()
    {
        $id = $this->request->param('id');
        $member = Db::name('member_member')->where('id',$id)->find();
        $this->assign('member',$member);
        return $this->fetch();
    }

    /**
     * 编辑
     */
    public function editPost()
    {
        $now = time();
        $params = $this->request->param();
        $params['mark'] = 2000;
        $params['status'] = 1000;
        $params['update_time'] = $now;
        $res = Db::name('member_member')->update($params);
        if($res)
            return $this->success("更新成功！",url('AdminMember/edit',array('id'=>$params['id'])));
        else
            return $this->error('更新失败');
    }

    public function delete()
    {
        $params = $this->request->param();
        $where['id'] = ['in',$params['ids'] ];
        $res = Db::name('member_member')->where($where)->update([ 'status'=>1001,'delete_time'=>time() ]);
        if($res)
            return $this->success("删除成功！");
        else
            return $this->error('删除失败');
    }

}